
//author marcel
 
package ch.sfug.anim.transition
{
	import ch.sfug.anim.tween.property.PropertyTween;
	import ch.sfug.events.TweenEvent;
	
	import flash.display.Sprite;
	import flash.filters.ColorMatrixFilter;
	
	public class BrightnessTransition extends Transition
	{ 
		private var pt:PropertyTween;
		private var _mode:String;
		private var _ease:Function = null;
		public var _brightness:Number = 0;
		
		public function BrightnessTransition(clip:Sprite=null, time:Number=1000, type:String=TransitionType.OUT, mode:String = "dark")
		{
			super(clip, time, type);
			_mode = mode;
			
		}
		
		override protected function prepareForTransition():void 
		{
			super.prepareForTransition();
			
			pt = new PropertyTween(this, super.time);
			pt.addEventListener(TweenEvent.TWEEN_UPDATE, onTransitionUpdate);
			pt.addEventListener(TweenEvent.TWEEN_END, super.onTransitionEnd);
			
		}
		
	
		
		override public function onTransitionUpdate( event:TweenEvent ):void
		{
			var matrix:Array = new Array();
            matrix = matrix.concat([1, 0, 0, 0, _brightness]);
            matrix = matrix.concat([0, 1, 0, 0, _brightness]);
            matrix = matrix.concat([0, 0, 1, 0, _brightness]);
            matrix = matrix.concat([0, 0, 0, 1, 0]);
            
            
			var filter:ColorMatrixFilter = new ColorMatrixFilter(matrix);
            var filters:Array = new Array();
            filters.push(filter);
            super.clip.filters = filters;
            
			dispatchEvent(new TweenEvent(TweenEvent.TWEEN_UPDATE));
		}
		
		override public function startTransition():void 
		{
			prepareForTransition();
			super.onTransitionStart();
			
			if(_mode == "bright")
			{
				if(super.type == "out")
				{
					pt.tween( { _brightness:{ from: 0, to: 255, easing: _ease} } );
				}
				else
				{
					pt.tween( { _brightness:{ from: 255, to: 0, easing: _ease} } );
				}
			}
			else if(_mode == "dark")
			{
				if(super.type == "out")
				{
					pt.tween( { _brightness:{ from: 0, to: -255, easing: _ease} } );
				}
				else
				{
					pt.tween( { _brightness:{ from: -255, to: 0, easing: _ease} } );
				}
			}
			
		}
		
		override public function stopTransition():void 
		{
			if(pt != null)
			{
				pt.stop();
				pt.removeEventListener(TweenEvent.TWEEN_UPDATE, super.onTransitionUpdate);
				pt.removeEventListener(TweenEvent.TWEEN_END, super.onTransitionEnd);
				pt = null;
			}
			
			super.cleanUp();
		}
		
		// getters and setters
		public function set ease( val:Function ):void
		{
			_ease = val;
		}
		
	}
}